library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
fig <- plot_ly(
    type = "sankey",
    orientation = "h",

    node = list(
      label = c("All UC Events", 
                "Long Term", 
                "Short Term", 
                "Case Filings", 
                "Cases Closed", 
                "MCH", 
                "State Court Hearings", 
                "LT Rep Initiated", 
                "KYRs", 
                "Initial Legal Screenings", 
                "ST Rep Initiated", 
                "VD Cases", 
                "I-360s", 
                "I-485s", 
                "I-589s", 
                "I-765s",
                "I-131s",
                "N-600s"),
      color = c("grey", 
                "grey", 
                "grey", 
                "rgba(255, 0, 0, 0.6)"),
      pad = 15,
      thickness = 20,
      line = list(
        color = "black",
        width = 0.5
      )
    ),

    link = list(
      source = c(0,0,1,3,3,3,3,1,1,1,1,2,2,2,2,3,3),
      target = c(1,2,3,12,13,14,15,4,5,6,7,8,9,10,11,16,17),
      value =  c(518,717,226,52,4,63,104,40,94,23,135,331,350,33,3,2,1),
      color = c("rgba(0, 113, 188, 0.6",    # Short Term (Darker Blue)
         "rgba(0, 113, 188, 0.6)",     # Long Term (Darker Blue)
         "rgba(255, 0, 0, 0.6)",  # Case Filings (Light Red)
         "rgba(204, 0, 0, 0.6)",       # 360 (Dark Red)
         "rgba(255, 128, 0, 0.6)",    # 485 (Orange)
         "rgba(255, 51, 153, 0.6)",   # 589 (Pink)
         "rgba(255, 68, 68, 0.6)",    # 765 (Bright Red)
         "rgba(139, 0, 0, 0.6)",  # Case Closed (Megenta)
         "rgba(255, 215, 0, 0.6)",     # MCH (Green)
         "rgba(255, 187, 51, 0.6)",   # State Court Hearings (Gold)
         "rgba(255, 128, 0, 0.6)",    # LT Rep Initiated (Orange)
         "rgba(102, 204, 0, 0.6)",    # KYR (Green)
         "rgba(0, 128, 128, 0.6)",    # Initial Legal Screening (Teal)
         "rgba(34, 139, 34, 0.6)",  # ST Rep Initiated (Light Blue)
         "rgba(102, 153, 255, 0.6)",
         "rgba(255, 165, 0, 0.6)",  # New Filing Type 1 (Orange)
         "rgba(255, 204, 153, 0.6)")  # New Filing Type 2 (Light Orange))   # VD Cases (Yellow-Orange)
  
    )
  )
#think of each column as a single stream, 1st value in the column is where it starts, second is where it goes, and 3rd value is how big it is 
# X
# Y
# Z


fig <- fig %>% layout(
    title = "Sankey Diagram of UC MIRC Events and Filings 01/01/23 - 06/30/23",
    font = list(
      size = 11
    )
)

fig